package com.service.scheduling.task;

import com.service.dao.enums.CoinsEnum;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.enums.OrderProductOtherStatus;
import com.service.dao.mapper.MemberMapper;
import com.service.dao.mapper.OrderProductHrComboMapper;
import com.service.dao.model.Member;
import com.service.dao.model.OrderProductHrCombo;
import com.service.scheduling.service.*;
import com.service.scheduling.support.MemberStoreLockUtils;
import com.service.scheduling.utils.MemCacheNoTran;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Component
@SuppressWarnings("all")
public class MemberGradeRewardTask {

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderProductHrComboMapper orderProductHrComboMapper;

    @Autowired
    private MemCacheNoTran memCache;

    private static BigDecimal baseNum = new BigDecimal(100L);

//    @Scheduled(cron = "0 */1 * * * ?")
    public void checkGradeLevel() {
        log.info("用户等级检查定时任务-开始");
        List<Member> memberList = memberService.listAll();
        if(ObjectUtils.isEmpty(memberList)){
            log.info("暂无用户列表");
            return;
        }
        memberList.parallelStream().forEach(item ->{
            //直推总业绩
            BigDecimal totalDirectInvitationPowerNums = BigDecimal.ZERO;
            //直推总人数
            int totalDirectInviteMember = 0;
            //间推总业绩
            BigDecimal totalIndirectInvitePowerNums = BigDecimal.ZERO;
            //间推总人数
            int totalIndirectInviteMembers = 0;
            //团队总业绩
            BigDecimal teamPowers = BigDecimal.ZERO;
            //团队总人数
            Integer teamMembers = 0;
            //判断当前用户是否有算力订单
            BigDecimal totalMyEffectPowerNums = orderProductHrComboMapper.totalEffectiveOrderByMemberId(item.getId(), CoinsEnum.FIL.getCode(), OrderProductOtherStatus.DELIVERED.getCode());
            if(totalMyEffectPowerNums.compareTo(BigDecimal.ZERO) <= 0){
                log.info("用户<memberId:{}>没有有效算力订单",item.getId());
                return;
            }
            //获取当前用户有效直邀人ID列表
            List<Integer> directInvitationIds = memberMapper.listMemberDirectInvitationIdsByParam(item.getId(), CommonStatusEnums.ACTIVE.getCode());
            if(!ObjectUtils.isEmpty(directInvitationIds)){
                for(Integer directInvitationMemberId:directInvitationIds){
                    //当前直推用户业绩查询
                    BigDecimal totalEffectPowerNums = orderProductHrComboMapper.totalEffectiveOrderByMemberId(directInvitationMemberId, CoinsEnum.FIL.getCode(), OrderProductOtherStatus.DELIVERED.getCode());
                    if(totalEffectPowerNums.compareTo(BigDecimal.ZERO) > 0){
                        totalDirectInvitationPowerNums.add(totalEffectPowerNums);//每个直推用户的业绩添加到直推总业绩中
                        totalDirectInviteMember = totalDirectInviteMember + 1;//有效直推人数加1
                        teamMembers = teamMembers + 1;//团队总人数+1
                        teamPowers = teamPowers.add(totalEffectPowerNums);//团队总业绩增加
                    }else{
                        continue;
                    }
                    //通过直邀用户的id获取间邀用户id列表
                    List<Integer> indirectInviteIds = memberMapper.listMemberDirectInvitationIdsByParam(item.getId(),CommonStatusEnums.ACTIVE.getCode());
                    if(!ObjectUtils.isEmpty(indirectInviteIds)){
                        for(Integer indirectInviteId: indirectInviteIds){
                            //间推用户的有效算力
                            BigDecimal totalindirectInviteEffectPowerNums = orderProductHrComboMapper.totalEffectiveOrderByMemberId(indirectInviteId, CoinsEnum.FIL.getCode(), OrderProductOtherStatus.DELIVERED.getCode());
                            if(totalindirectInviteEffectPowerNums.compareTo(BigDecimal.ZERO) > 0){
                                totalIndirectInvitePowerNums.add(totalindirectInviteEffectPowerNums);//每个间推推用户的业绩添加到间推推总业绩中
                                totalIndirectInviteMembers = totalIndirectInviteMembers + 1;//有效间推推人数加1
                                teamMembers = teamMembers + 1;//团队总人数+1
                                teamPowers = teamPowers.add(totalindirectInviteEffectPowerNums);//团队总业绩增加
                            }else{
                                continue;
                            }
                        }
                    }
                }
            }
            if(teamPowers.compareTo(BigDecimal.ZERO) > 0 && teamPowers.compareTo(new BigDecimal("200")) < 0){
                item.setVipGrade(0);
            }else if(teamPowers.compareTo(new BigDecimal("200")) >= 0 && teamPowers.compareTo(new BigDecimal("500")) < 0){
                item.setVipGrade(1);
            }else if(teamPowers.compareTo(new BigDecimal("500")) >= 0){
                item.setVipGrade(2);
            }
            memberService.updateById(item);
        });
        log.info("用户等级检查定时任务-结束");
    }
}
